package com.pawelsekara.curves;

import java.util.HashMap;
import java.util.Map;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.ZoomCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.opengl.util.GLState;
import org.andengine.ui.activity.BaseGameActivity;

import android.graphics.Color;
import android.util.Log;

public class CurvesGame extends BaseGameActivity {

	public static int CAMERA_WIDTH;
	public static int CAMERA_HEIGHT;

	private ZoomCamera mCamera;

	public BuildableBitmapTextureAtlas mainMenuAtlas;
	public BuildableBitmapTextureAtlas levelAtlas;

	public Map<String, TextureRegion> levelMap;
	public Map<String, TextureRegion> mainMenuMap;
	public Map<String, Font> fontsMap;

	private BitmapTextureAtlas loadingAtlas;
	private TextureRegion loadingRegion;

	private void loadLevelTextures() {
		levelAtlas = new BuildableBitmapTextureAtlas(getTextureManager(), 1024,
				1024, TextureOptions.REPEATING_BILINEAR_PREMULTIPLYALPHA);
		levelMap = new HashMap<String, TextureRegion>();

		levelMap.put("levelselectbg", BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(levelAtlas, this, "levelselectbg.png"));
		
		try {
			levelAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							1, 4, 1));
			levelAtlas.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}

	}

	private void loadFonts() {
		FontFactory.setAssetBasePath("fonts/");
		fontsMap = new HashMap<String, Font>();
		BitmapTextureAtlas fontAtlas = new BitmapTextureAtlas(
				getTextureManager(), 512, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		float fontSizeScaled = Float.valueOf(CurvesGame.CAMERA_HEIGHT)
				/ Constants.MAX_SCREEN_HEIGHT * Constants.MAX_FONT_SIZE;
		Font tFont = FontFactory.createFromAsset(getFontManager(), fontAtlas,
				getAssets(), Constants.FONT1_NAME + ".ttf", fontSizeScaled,
				true, Color.WHITE);
		tFont.load();
		fontsMap.put(Constants.FONT1_NAME, tFont);
		mEngine.getTextureManager().loadTexture(fontAtlas);
	}

	private void loadMainMenuTextures() {
		mainMenuAtlas = new BuildableBitmapTextureAtlas(getTextureManager(),
				2048, 2048, TextureOptions.BILINEAR);
		mainMenuMap = new HashMap<String, TextureRegion>();

		mainMenuMap.put("mainmenubg", BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(mainMenuAtlas, this, "mainmenubg.png"));

		try {
			mainMenuAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							1, 4, 1));
			mainMenuAtlas.load();
		} catch (TextureAtlasBuilderException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onPopulateScene(Scene scene,
			OnPopulateSceneCallback onPopulateSceneCallback) throws Exception {
		float width, height, ratio;
		if (CAMERA_WIDTH < CAMERA_HEIGHT) {
			ratio = CAMERA_WIDTH / Float.valueOf(loadingRegion.getWidth());
		} else {
			ratio = CAMERA_HEIGHT / Float.valueOf(loadingRegion.getHeight());
		}
		width = ratio * loadingRegion.getWidth();
		height = ratio * loadingRegion.getHeight();

		Log.i("LoadingScreen", "Loading Screen Dimenstions " + width + " X "
				+ height);

		scene.attachChild(new Sprite((CAMERA_WIDTH - width) / 2,
				(CAMERA_HEIGHT - height) / 2, width, height, loadingRegion,
				getVertexBufferObjectManager()) {
			@Override
			protected void preDraw(GLState pGLState, Camera pCamera) {
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		});
		scene.setBackground(new Background(1, 1, 1));

		loadFonts();
		loadMainMenuTextures();
		loadLevelTextures();

		SceneManager.getManager().setScene(new MainMenuScene(mCamera));

		onPopulateSceneCallback.onPopulateSceneFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback onCreateSceneCallback)
			throws Exception {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		onCreateSceneCallback.onCreateSceneFinished(new Scene());
	}

	@Override
	public EngineOptions onCreateEngineOptions() {

		CAMERA_HEIGHT = getWindowManager().getDefaultDisplay().getHeight();
		CAMERA_WIDTH = getWindowManager().getDefaultDisplay().getWidth();

		mCamera = new ZoomCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		final EngineOptions engineoptions = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
						CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
		engineoptions.getRenderOptions().setDithering(true);

		Log.i("Dimensions", "Cam_H X Cam_W : " + CAMERA_HEIGHT + " X "
				+ CAMERA_WIDTH);

		return engineoptions;
	}

	@Override
	public void onCreateResources(
			OnCreateResourcesCallback onCreateResourcesCallback)
			throws Exception {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		loadingAtlas = new BitmapTextureAtlas(getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		loadingRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				loadingAtlas, this, "loading.png", 0, 0);
		getTextureManager().loadTexture(loadingAtlas);

		SceneManager.init(this);
		onCreateResourcesCallback.onCreateResourcesFinished();
	}
	
	public ZoomCamera getCamera () {
		return mCamera;
	}

	@Override
	protected void onPause() {
		super.onPause();

		System.gc();
	}

	@Override
	protected synchronized void onResume() {
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

	}
}
